﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace IceDog.Practice.WinForm.Common
{
    class FormRichTextBoxTestCore
    {
        private FormRichTextBoxTestCore() { }
        public FormRichTextBoxTestCore(RichTextBox rtb)
        {
            this._richTextBox = rtb;
        }
        private RichTextBox _richTextBox;
        internal void SetFontStyle(FontStyle style)
        {
            var rtb = this._richTextBox;
            var oldFont = rtb.SelectionFont;
            Font newFont;
            if ((oldFont.Style&style)==style)//判断当前字体样式是否包含此样式
            {
                //包含则排除当前样式值
                newFont = new Font(oldFont, oldFont.Style & ~style);
            }
            else
            {
                //不包含则添加此样式
                newFont = new Font(oldFont, oldFont.Style | style);
            }
            rtb.SelectionFont = newFont;
            rtb.Focus();
        }
        internal void GetFontsizeList(ToolStripComboBox box)
        {
            for (int i = 12; i < 50; i++)
            {
                box.Items.Add(i);
            }
        }
        internal void SetFontsize(int size)
        {
            var rtb = this._richTextBox;
            var old = rtb.SelectionFont.FontFamily;
            var newFont = new Font(old, size);
            rtb.SelectionFont = newFont;
            rtb.Focus();
        }
        internal void SetCenter()
        {
            if (this._richTextBox.SelectionAlignment == HorizontalAlignment.Center)
            {
                this._richTextBox.SelectionAlignment = HorizontalAlignment.Left;
            }
            else
            {
                this._richTextBox.SelectionAlignment = HorizontalAlignment.Center;
            }
            this._richTextBox.Focus();
        }
        internal void LoadFile()
        {
            try
            {
                this._richTextBox.LoadFile("file.rtf");
            }
            catch (System.IO.FileNotFoundException e)
            {
                MessageBox.Show("file.rtf not found"+e);
            }
        }
        internal void SaveFile()
        {
            try
            {
                this._richTextBox.SaveFile("file.rtf");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
    }
}
